package com.peanut.baby.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.CheckBox;

import com.peanut.baby.R;


public class LeftCheckBox extends CheckBox {
    private static final int DEFAULT_SIZE = 16;
    float size;

    public LeftCheckBox(Context context, AttributeSet attrs,
                        int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public LeftCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public LeftCheckBox(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context ctx, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typeArray = ctx.obtainStyledAttributes(attrs,
                    R.styleable.CustomView);
            //从typeArray获取相应值，第二个参数为默认值，如第一个参数在atts.xml中没有定义，返回第二个参数值
            size = typeArray.getDimension(R.styleable.CustomView_size, 16);

            typeArray.recycle();
        } else {
            size = dip2px(ctx, DEFAULT_SIZE);
        }
        Drawable drawable = getCompoundDrawables()[0];

        if(drawable != null){
            drawable.setBounds(0, 0, (int) size, (int) size);
            setCompoundDrawables(drawable,null, null, null);
        }

    }


    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}
